package zhihui.mobile.ui.express;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhihui.mobile.R;
import zhihui.mobile.bean.express.ExpressTimeBeanListV2;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.express.ExpressManager;
import zhihui.mobile.manager.user.UserManager;


public class SelectExpressTimeDialog {

    @Bind(R.id.express_select_time_1_tv)
    TextView expressSelectTime1Tv;
    @Bind(R.id.express_select_time_2_tv)
    TextView expressSelectTime2Tv;
    @Bind(R.id.express_select_time_3_tv)
    TextView expressSelectTime3Tv;
    @Bind(R.id.express_select_time_rv)
    RecyclerView expressSelectTimeRv;


    private Activity activity;
    private LayoutInflater inflater;
    private View view;
    private Dialog shareDialog;
    private ExpressSelectTimeAdapter expressSelectTimeAdapter;

    private int day = 0;
    private int dayHour = -1;

    public void setDay(int day) {
        this.day = day;
        if (day == 0) {
            return;
        }
        switch (day) {
            case 1:
                expressSelectTime1Tv.setSelected(true);
                expressSelectTime2Tv.setSelected(false);
                expressSelectTime3Tv.setSelected(false);
                expressSelectTime1Tv.setTextColor(0xffff6537);
                expressSelectTime2Tv.setTextColor(0xff333333);
                expressSelectTime3Tv.setTextColor(0xff333333);
                break;
            case 2:
                expressSelectTime1Tv.setSelected(false);
                expressSelectTime2Tv.setSelected(true);
                expressSelectTime3Tv.setSelected(false);
                expressSelectTime1Tv.setTextColor(0xff333333);
                expressSelectTime2Tv.setTextColor(0xffff6537);
                expressSelectTime3Tv.setTextColor(0xff333333);
                break;
            case 3:
                expressSelectTime1Tv.setSelected(false);
                expressSelectTime2Tv.setSelected(false);
                expressSelectTime3Tv.setSelected(true);
                expressSelectTime1Tv.setTextColor(0xff333333);
                expressSelectTime2Tv.setTextColor(0xff333333);
                expressSelectTime3Tv.setTextColor(0xffff6537);
                break;
        }

    }

    public void setDayHour(int dayHour) {
        this.dayHour = dayHour;
        if (dayHour == -1) {
            return;
        }
        if (null == expressSelectTimeAdapter) {
            return;
        }
        expressSelectTimeAdapter.getData().get(dayHour).setSelect(true);
        for (int i = 0; i < expressSelectTimeAdapter.getData().size(); i++) {

            if (i != dayHour) {
                expressSelectTimeAdapter.getData().get(i).setSelect(false);

            }
        }
        expressSelectTimeAdapter.notifyDataSetChanged();
    }

    public int getDay() {
        return day;
    }

    public int getDayHour() {
        return dayHour;
    }

    public SelectExpressTimeDialog(Activity activity) {
        this.activity = activity;
        this.inflater = LayoutInflater.from(activity);
    }


    public void createDialog() {
        view = inflater.inflate(R.layout.dialog_select_express, null);
        shareDialog = new AlertDialog.Builder(activity, R.style.SelectTimeStyle).create();
        shareDialog.show();
        ButterKnife.bind(this, view);
        Window window = shareDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        Display display = activity.getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = display.getWidth();
        window.setAttributes(layoutParams);
        window.setContentView(view);
    }

    private void initData(ExpressTimeBeanListV2 expressTimeBeanList) {
        expressSelectTimeRv.setLayoutManager(new GridLayoutManager(activity, 3));
        expressSelectTimeRv.setHasFixedSize(true);
        expressSelectTimeAdapter = new ExpressSelectTimeAdapter(expressTimeBeanList.getData(), activity);
        expressSelectTimeRv.setAdapter(expressSelectTimeAdapter);
    }

    @OnClick({R.id.express_select_time_1_tv, R.id.express_select_time_2_tv, R.id.express_select_time_3_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.express_select_time_1_tv:
                expressSelectTime1Tv.setSelected(true);
                expressSelectTime2Tv.setSelected(false);
                expressSelectTime3Tv.setSelected(false);
                expressSelectTime1Tv.setTextColor(0xffff6537);
                expressSelectTime2Tv.setTextColor(0xff333333);
                expressSelectTime3Tv.setTextColor(0xff333333);
                day = 1;
                dayHour = -1;
                EventBus.getDefault().post(new BaseEventBus("day", day));
//                EventBus.getDefault().post(new BaseEventBus("dayHour", dayHour));
//                EventBus.getDefault().post(new BaseEventBus("dayHourId", expressSelectTimeAdapter.getData().get(position).getId()));
                getTime(1);
                break;
            case R.id.express_select_time_2_tv:
                expressSelectTime1Tv.setSelected(false);
                expressSelectTime2Tv.setSelected(true);
                expressSelectTime3Tv.setSelected(false);
                expressSelectTime1Tv.setTextColor(0xff333333);
                expressSelectTime2Tv.setTextColor(0xffff6537);
                expressSelectTime3Tv.setTextColor(0xff333333);
                day = 2;
                dayHour = -1;
                EventBus.getDefault().post(new BaseEventBus("day", day));
//                EventBus.getDefault().post(new BaseEventBus("dayHour", dayHour));
//                EventBus.getDefault().post(new BaseEventBus("dayHourId", expressSelectTimeAdapter.getData().get(position).getId()));
                getTime(2);
                break;
            case R.id.express_select_time_3_tv:
                expressSelectTime1Tv.setSelected(false);
                expressSelectTime2Tv.setSelected(false);
                expressSelectTime3Tv.setSelected(true);
                expressSelectTime1Tv.setTextColor(0xff333333);
                expressSelectTime2Tv.setTextColor(0xff333333);
                expressSelectTime3Tv.setTextColor(0xffff6537);
                day = 3;
                dayHour = -1;
                EventBus.getDefault().post(new BaseEventBus("day", day));
//                EventBus.getDefault().post(new BaseEventBus("dayHour", dayHour));
//                EventBus.getDefault().post(new BaseEventBus("dayHourId", expressSelectTimeAdapter.getData().get(position).getId()));
                getTime(3);
                break;
        }
    }

    private void getTime(int isToday) {
        ExpressManager.getInstance().initGetShopSendTimeList(new IHttpCallBackListener() {
            @Override
            public void success(JSONObject result) {
                Gson gson = new Gson();
                ExpressTimeBeanListV2 expressTimeBeanList = gson.fromJson(result.toString(), ExpressTimeBeanListV2.class);
                initData(expressTimeBeanList);
            }

            @Override
            public void failed(String message) {
                com.zgm.utils.UiUtils.ToastInCenter(activity, message, Toast.LENGTH_SHORT);
            }
        }, UserManager.getInstance().getCurUser().getStore(), isToday);
    }

    @OnClick(R.id.express_select_time_commit_btn)
    public void onViewClicked() {
        if (day == 0 || dayHour == -1) {
            com.zgm.utils.UiUtils.ToastInCenter(activity, "请选择派送时间", Toast.LENGTH_SHORT);
            return;
        }
        shareDialog.dismiss();
    }

    public class ExpressSelectTimeAdapter extends BaseQuickAdapter<ExpressTimeBeanListV2.DataBean, BaseViewHolder> {

        private Context context;

        public ExpressSelectTimeAdapter(List data, Context context) {
            super(R.layout.item_express_select_time, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, ExpressTimeBeanListV2.DataBean item) {
            helper.setText(R.id.express_select_time_tv, item.getStart() + "-" + item.getEnd());
            if (item.isSelect()) {
                helper.getView(R.id.express_select_time_tv).setSelected(true);
                helper.setTextColor(R.id.express_select_time_tv, 0xffff6537);
            } else {
                helper.getView(R.id.express_select_time_tv).setSelected(false);
                helper.setTextColor(R.id.express_select_time_tv, 0xff333333);
            }
            setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    for (int i = 0; i < mData.size(); i++) {
                        if (i != position) {
                            mData.get(i).setSelect(false);
                        }
                    }
                    mData.get(position).setSelect(true);
                    dayHour = position;
                    EventBus.getDefault().post(new BaseEventBus("day", day));
                    EventBus.getDefault().post(new BaseEventBus("dayHour", dayHour, expressSelectTimeAdapter.getData()));
                    EventBus.getDefault().post(new BaseEventBus("dayHourId", expressSelectTimeAdapter.getData().get(position).getId()));
                    notifyDataSetChanged();
                }
            });
        }
    }


}
